from django.db import models

# Create your models here.

class Test(models.Model):
    A = models.CharField(max_length=2)
    B = models.CharField(max_length=2)
    
    def __unicode__(self):
        return u"%s | %s" % (self.A, self.B)
    
class TestAllBaseTypes(models.Model):
    AutoField = models.AutoField(primary_key=True) # int
    BooleanField = models.BooleanField() # bool
    CharField = models.CharField(max_length=50) # unicode
    CommaSeparatedIntegerField = models.CommaSeparatedIntegerField(max_length=25) # unicode
    DateField = models.DateField(auto_now_add=True) # datetime.date
    DateTimeField = models.DateTimeField(auto_now_add=True) # datetime.datetime
    DecimalField = models.DecimalField(max_digits=10,decimal_places=2) # decimal.Decimal
    EmailField = models.EmailField(max_length=75) # unicode
    #FileField = models.FileField()
    #FilePathField = models.FilePathField()
    FloatField = models.FloatField() # float
    #ImageField = models.ImageField() 
    IntegerField = models.IntegerField() # int
    IPAddressField = models.IPAddressField() # unicode
    NullBooleanField = models.NullBooleanField() # bool
    PositiveIntegerField = models.PositiveIntegerField() # int
    PositiveSmallIntegerField = models.PositiveSmallIntegerField() # int
    SlugField = models.SlugField(max_length=30) # unicode
    SmallIntegerField = models.SmallIntegerField() # int
    TextField = models.TextField() # unicode
    TimeField = models.TimeField(auto_now_add=True) # datetime.time
    URLField = models.URLField(verify_exists=False,max_length=100) # unicode
    #XMLField = models.XMLField()
    def __unicode__(self):
        return u"%s" % (self.CharField)


class TestRelated(models.Model):
    BaseTypes = models.ManyToManyField(TestAllBaseTypes)
    Test = models.ForeignKey(Test)
    DateField = models.DateField(auto_now_add=True) # datetime.date
    DateTimeField = models.DateTimeField(auto_now_add=True) # datetime.datetime
    TimeField = models.TimeField(auto_now_add=True) # datetime.time



#####################################################################

class Category(models.Model):
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class Book(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category)
    publish_date = models.DateField()
    about = models.TextField()
    #For later use maybe
    #FIXME - consider adding ManyToManyField
#     cover = models.ImageField(null=True, blank=True,
#                               upload_to='book_covers')
    chapters = models.ManyToManyField("Chapter",related_name="Book_Chapter")
    bought_on = models.DateField()
    price = models.DecimalField(max_digits=7,
                                decimal_places=2)
    ebook = models.BooleanField(verbose_name="E-Book")

    def __unicode__(self):
        return self.name

    def _get_authors(self):
        return self.chapter_set.object.all()
    authors = property(_get_authors)


class Author(models.Model):
    name = models.CharField(max_length=100)
    bio = models.TextField()
    dob = models.DateField()

    def __unicode__(self):
        return self.name

    def books_written(self):
        return self.book_set.objects.all().count()

class Chapter(models.Model):
    name = models.CharField(max_length=100)
    book = models.ForeignKey(Book)
    author = models.ForeignKey(Author)
    word_count = models.PositiveIntegerField()
    content = models.TextField()

    def __unicode__(self):
        return self.name
